/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#include "WorldGUI.hpp"

#include <QuickGUI.h>

#include "Game.hpp"
#include "SquadSelection.hpp"
#include "WorldManager.hpp"
#include "WorldScene.hpp"

using namespace QuickGUI;

namespace spelprog2 {
namespace gui {

typedef QuickGUI::Rect GUIRect;

WorldGUI::WorldGUI(Game & game, Ogre::SceneManager & sceneMan,
    presentation::WorldScene & worldScene) : GameGUI(sceneMan, game.getViewport()),
    game_(game),
    scene_(worldScene)
{
    initPlaySheet();

    getManager().setActiveSheet(&get<Sheet>("playSheet"));
}

WorldGUI::~WorldGUI()
{
    //manually destroy anything containing text boxes as QuickGUI 10.1 otherwise crashes
    destroy("playSheet.chatTextRect");
}

void WorldGUI::initPlaySheet()
{
    const FloatRect allSheet = { 0.01f, 0.01f, 0.99f, 0.99f };
    Sheet & sheet = *createSheet(allSheet);
    add("playSheet", sheet);
    GUIRect sheetDim = sheet.getClientDimensions();
    sheetDim.position = QuickGUI::Point::ZERO;
    
    // Squad orders panel and its buttons =====================================

    const FloatRect squadOrdersPanelRect = { 0.0f, 0.92f, 1.0f, 1.0f };
    Panel & squadOrdersPanel = *sheet.createPanel(layoutRect(squadOrdersPanelRect, sheetDim));
    GUIRect squadOrdersPanelDim = squadOrdersPanel.getClientDimensions();
    squadOrdersPanelDim.position = QuickGUI::Point::ZERO;

    Ogre::Real buttonX = 0;
    const Ogre::Real buttonSize = squadOrdersPanelDim.size.height;
    Button & denseFormationButton = *squadOrdersPanel.createButton(GUIRect(buttonX, 0, buttonSize, buttonSize));
    denseFormationButton.setImage("DenseFormation.png");
    denseFormationButton.addWidgetEventHandler(WIDGET_EVENT_MOUSE_BUTTON_UP, &WorldGUI::clickDenseFormationButton, this);
    buttonX += buttonSize;
    Button & looseFormationButton = *squadOrdersPanel.createButton(GUIRect(buttonX, 0, buttonSize, buttonSize));
    looseFormationButton.setImage("LooseFormation.png");
    looseFormationButton.addWidgetEventHandler(WIDGET_EVENT_MOUSE_BUTTON_UP, &WorldGUI::clickLooseFormationButton, this);
    buttonX += buttonSize;
    Button & wedgeFormationButton = *squadOrdersPanel.createButton(GUIRect(buttonX, 0, buttonSize, buttonSize));
    wedgeFormationButton.setImage("WedgeFormation.png");
    wedgeFormationButton.addWidgetEventHandler(WIDGET_EVENT_MOUSE_BUTTON_UP, &WorldGUI::clickWedgeFormationButton, this);
    buttonX += buttonSize;

    buttonX += buttonSize * 0.2f;
    Button & stopOrderButton = *squadOrdersPanel.createButton(GUIRect(buttonX, 0, buttonSize, buttonSize));
    stopOrderButton.setImage("Stop.png");
    stopOrderButton.addWidgetEventHandler(WIDGET_EVENT_MOUSE_BUTTON_UP, &WorldGUI::clickStopOrderButton, this);
    buttonX += buttonSize;
    Button & toggleHurryButton = *squadOrdersPanel.createButton(GUIRect(buttonX, 0, buttonSize, buttonSize));
    toggleHurryButton.setImage("Hurry.png");
    toggleHurryButton.addWidgetEventHandler(WIDGET_EVENT_MOUSE_BUTTON_UP, &WorldGUI::clickToggleHurryButton, this);
    buttonX += buttonSize;

    // Chat list (display chat for a while) ===================================

    const FloatRect chatListRect = { 0.0f, 0.1f, 0.6f, 0.8f };
    List & chatList = *sheet.createList(layoutRect(chatListRect, sheetDim));
    add("playSheet.chatList", chatList);
    chatList.setEnabled(false); //don't want user selected list items
    chatList.setRelativeOpacity(0);
    
    // Chat panel (for entering chat text) ====================================

    const FloatRect chatPanelRect = { 0.0f, 0.0, 1.0f, 0.06f };
    GUIRect chatPanelFinalRect = layoutRect(chatPanelRect, sheetDim);
    chatPanelFinalRect.size.height = 46; //height is fixed to avoid unpleasant surprises
    Panel & chatPanel = *sheet.createPanel(chatPanelFinalRect);
    add("playSheet.chatPanel", chatPanel);
    GUIRect chatPanelDim = chatPanel.getClientDimensions();
    chatPanelDim.position = QuickGUI::Point::ZERO;

    const float CHAT_LABEL_RECT_WIDTH = 65;
    GUIRect chatLabelRect = chatPanelDim;
    chatLabelRect.size.width = CHAT_LABEL_RECT_WIDTH;
    Label & chatLabel = *chatPanel.createLabel("Team talk:", chatLabelRect);
    add("playSheet.chatLabel", chatLabel);

    GUIRect chatTextRect = chatPanelDim;
    chatTextRect.size.width -= CHAT_LABEL_RECT_WIDTH;
    chatTextRect.position.x = CHAT_LABEL_RECT_WIDTH;
    TextBox & chatText = *chatPanel.createTextBox(chatTextRect);
    add("playSheet.chatTextRect", chatText);
}

void WorldGUI::clickDenseFormationButton(const QuickGUI::EventArgs & ev)
{
    tryChangeFormation(auxmsg::DENSE_LINE);
}

void WorldGUI::clickLooseFormationButton(const QuickGUI::EventArgs & ev)
{
    tryChangeFormation(auxmsg::LOOSE_LINE);
}

void WorldGUI::clickWedgeFormationButton(const QuickGUI::EventArgs & ev)
{
    tryChangeFormation(auxmsg::WEDGE);
}

void WorldGUI::clickStopOrderButton(const QuickGUI::EventArgs & ev)
{
    if (scene_.friendlySquadsSelected()) {
        presentation::SquadSelections::ConstIteratorRange range = scene_.getSelectedSquads();

        for (presentation::SquadSelections::ConstIterator it = range.first; it != range.second; ++it) {
            game_.getWorldManager().tryStopSquad(it->getSquad());
        }
    }
}

void WorldGUI::clickToggleHurryButton(const QuickGUI::EventArgs & ev)
{
}

void WorldGUI::tryChangeFormation(auxmsg::Formation formation)
{
    if (scene_.friendlySquadsSelected()) {
        presentation::SquadSelections::ConstIteratorRange range = scene_.getSelectedSquads();

        for (presentation::SquadSelections::ConstIterator it = range.first; it != range.second; ++it) {
            game_.getWorldManager().tryChangeFormation(it->getSquad(), formation);
        }
    }
}

}; //gui
}; //spelprog2
